<?php
function AconListFolderData($mes) {
  global $_GET, $path, $style, $session_id, $data, $term_Thing_ID, $language, $header;
  $filename = $path[$style]["tpl"]."/attrdata.xml";
  $fp = fopen($filename, "r");
  $contents = fread($fp, filesize ($filename));
  fclose ($fp);

  preg_match("/\%REPEAT\%(.*?)\%REPEAT\%/s", $contents, $matches);
  preg_match("/\%REPDAT\%(.*?)\%REPDAT\%/s", $matches[1], $datmatches);

  $udata = str_replace($datmatches[0], "", $matches[1]);
  $udata = str_replace("%GROUP_ID%", "0", $udata);
  $udata = str_replace("%GROUP_TL%", $mes, $udata);
  $contents = str_replace("%DatHref%", "index.php?trg=ontoedit&amp;act=aconlistdata&amp;sid=".
              $session_id."&amp;uid=".$_GET["uid"], $contents);

  header ($header["xml"]);
  return str_replace($matches[0], $udata, $contents);
}

function getAcon($class_id, $matches, $session_id){
  global $_GET, $undef_id, $language;
  $next_class_id = $class_id;
  $parent_class_id = $next_class_id;
  #$sql_str = "SELECT t.text FROM class c, term t WHERE t.id=c.term_id and c.id=".$class_id;
  $sql_str = "SELECT t.text FROM class c, term t WHERE t.id=c.term_id and c.id=".$class_id." and t.lang_id = ".$language["content"];
  getData($sql_str, $next_data);
  $inherited = false;
  $rep_cont = "";
  while ($next_data["text"] != "Thing"){
    $sql_str = "SELECT class2_id as class2_id, text FROM acon a, term t, class c WHERE (a.class2_id = c.id and c.term_id = t.id and t.lang_id = ".$language["content"]." and a.class1_id = ".$next_class_id.") UNION SELECT class1_id as class2_id, text FROM acon a, term t, class c WHERE (a.class1_id=c.id and c.term_id=t.id and t.lang_id = ".$language["content"]." and a.class2_id=".$next_class_id.")";
    $i_max = getMultData($sql_str, $ontosdata);

    $sql_str = "SELECT * FROM acon a, term t WHERE t.id=".$undef_id." and t.lang_id = ".$language["content"]." and a.class2_id=-1 and a.class1_id=".$next_class_id;
    if (getData($sql_str, $ontosdata1) == 1){
      $ontosdata[$i_max]["class2_id"] = $ontosdata1["class2_id"];
      $ontosdata[$i_max]["text"] = $ontosdata1["text"];
      $i_max++;
    };

    for ($i = 0; $i < $i_max; $i++) {
      $tmp_attr = str_replace("%CLASS_ID%", "ac".$parent_class_id."ac".$ontosdata[$i]["class2_id"], $matches[1]);

      if (isset($_GET["sel"]) && $_GET["sel"] == "ac".$parent_class_id."ac".$ontosdata[$i]["class2_id"])
        $tmp_attr = str_replace("%ACT%", "select", $tmp_attr);

      $tmp_attr = str_replace("%CLASS_NM%", $ontosdata[$i]["text"], $tmp_attr);
      if ($inherited)
        $tmp_attr = str_replace("%IMG_TYPE%", "inhattr", $tmp_attr);
      else
        $tmp_attr = str_replace("%IMG_TYPE%", "attr", $tmp_attr);

      if (isset($_GET["sel"]) &&
        ($_GET["sel"] == "ac".$parent_class_id."ac".$ontosdata[$i]["class2_id"] ||
        ($_GET["sel"] == 0 && $i == 0))) {
        $tmp_attr = str_replace("%ACT%", "select", $tmp_attr);
      }
      $tmp_attr = str_replace("%CLASS_LN%", "index.php?trg=ontoedit&amp;act=acondata&amp;sid=".$session_id."&amp;uid="."ac".$parent_class_id."ac".$ontosdata[$i]["class2_id"], $tmp_attr);
      $tmp_attr = str_replace("%CHILD_LN%", "index.php?trg=ontoedit&amp;act=aconfunctional&amp;sid=".$session_id."&amp;uid="."ac".$parent_class_id."ac".$ontosdata[$i]["class2_id"], $tmp_attr);
      $rep_cont .= $tmp_attr;
    }
    $sql_str = "SELECT parent_id FROM class WHERE id=".$next_class_id;
    getData($sql_str, $next_data);
    $next_class_id = $next_data["parent_id"];
    $sql_str = "SELECT t.text FROM class c, term t WHERE t.id=c.term_id and c.id=".$next_class_id;
    getData($sql_str, $next_data);
    $inherited = true;
  };
  return $rep_cont;
};

function aconList(){
 global $_GET, $data, $path, $style, $session_id, $language, $header;
  $filename = $path[$style]["tpl"]."/classes.xml";
  $fp = fopen($filename, "r");
  $contents = fread($fp, filesize ($filename));
  fclose ($fp);

  preg_match("/\%REPEAT\%(.*?)\%REPEAT\%/s", $contents, $matches);
  $rep_cont = getAcon(str_replace("ac","",$_GET["uid"]), $matches, $session_id);

  $contents = str_replace($matches[0], $rep_cont, $contents);
  header ($header["xml"]);
  return $contents;
};

function acon_functional(){
  global $_GET, $data, $path, $style, $session_id, $language, $header;

  include_once "functional.php";

  $filename = $path[$style]["tpl"]."/classes.xml";
  $fp = fopen($filename, "r");
  $contents = fread($fp, filesize ($filename));
  fclose ($fp);
  preg_match("/\%REPEAT\%(.*?)\%REPEAT\%/s", $contents, $matches);
  $classid = substr($_GET["uid"], 2, strpos(substr($_GET["uid"], 2), "ac"));
  $childclassid = substr($_GET["uid"], strpos(substr($_GET["uid"], 1), "ac") + 3);

  // functional constraints
  $tmp_attr = str_replace("%CLASS_ID%", "funalist".$classid."ac".$childclassid, $matches[1]);
  $tmp_attr = str_replace("%CLASS_NM%", getMessage("Functional constraints"), $tmp_attr);
  $tmp_attr = str_replace("%IMG_TYPE%", "folder", $tmp_attr);
  $tmp_attr = str_replace("%CLASS_LN%", "index.php?trg=ontoedit&amp;act=functionallistdata&amp;sid=".$session_id."&amp;uid=funalist".$classid."ac".$childclassid, $tmp_attr);

  if (getFunctional($classid, $matches, $session_id, "acon") != "")
    $tmp_attr = str_replace("%CHILD_LN%", "index.php?trg=ontoedit&amp;act=functionallist&amp;sid=".$session_id."&amp;uid=funalist".$classid."ac".$childclassid, $tmp_attr);
  else
    $tmp_attr = str_replace("%CHILD_LN%", "", $tmp_attr);
  $rep_cont .= $tmp_attr;
  // end FC
  $contents = str_replace($matches[0], $rep_cont, $contents);
  header ($header["xml"]);
  return $contents;
};

function get_child($parent, $bad_child, &$childs, &$count_childs, $ID){
  global $language;

  $i_max = getMultData("SELECT t.text, c.id FROM class c, term t ".
              "WHERE c.term_id=t.id and t.lang_id = ".$language["content"]." and c.parent_id=".$parent." and c.id<>".$bad_child, $related_class);

  for ($i = 0; $i < $i_max; $i++){
    //if (getdata("SELECT * FROM acon WHERE (class1_id=".$ID." and class2_id=".$related_class[$i]["id"].") or (class2_id=".$ID." and class1_id=".$related_class[$i]["id"].")", $data) != 1){
      $childs[$count_childs]["id"] = $related_class[$i]["id"];
      $childs[$count_childs]["text"] = $related_class[$i]["text"];
      $count_childs++;
    //};
    get_child($related_class[$i]["id"], 0, $childs, $count_childs, $ID);
  };
};

function my_acon_sort($childs){
  for ($i = 0; $i < count($childs) - 1; $i++){
    for ($j = 0; $j < count($childs) - 1; $j++){
      if (strcmp($childs[$j]["text"], $childs[$j + 1]["text"]) > 0){
        $time = $childs[$j]["text"];
        $childs[$j]["text"] = $childs[$j + 1]["text"];
        $childs[$j + 1]["text"] = $time;

        $time = $childs[$j]["id"];
        $childs[$j]["id"] = $childs[$j + 1]["id"];
        $childs[$j + 1]["id"] = $time;
      };
    };
  };
  return $childs;
};

function get_related_class(&$childs, $ID){
  getData("SELECT parent_id FROM class ".
          "WHERE id=".$ID, $related_class);
  $childs = Array();$count_childs = 0;
  if ($ID != "Thing")
    get_child($related_class["parent_id"], $ID, $childs, $count_childs, $ID);

  getData("SELECT t.text FROM class c, term t ".
          "WHERE c.term_id=t.id and c.id=".$related_class["parent_id"], $name_of_class);
  while ($name_of_class["text"] != "Thing"){

    $bad_class = $related_class["parent_id"];
    getData("SELECT c.parent_id, t.text FROM class c, term t ".
            "WHERE c.term_id=t.id and c.id=".$related_class["parent_id"], $related_class);
    get_child($related_class["parent_id"], $bad_class, $childs, $count_childs, $ID);
    getData("SELECT t.text FROM class c, term t ".
            "WHERE c.term_id=t.id and c.id=".$related_class["parent_id"], $name_of_class);
  };
  $childs = my_acon_sort($childs);
};

function aconData($new_id) {
  global $_GET, $path, $style, $session_id, $data, $term_Thing_ID, $language, $header;
  $filename = $path[$style]["tpl"]."/attrdata.xml";
  $fp = fopen($filename, "r");
  $contents = fread($fp, filesize ($filename));
  fclose ($fp);

  preg_match("/\%REPEAT\%(.*?)\%REPEAT\%/s", $contents, $matches);
  preg_match("/\%REPDAT\%(.*?)\%REPDAT\%/s", $matches[1], $datmatches);
  preg_match("/\%REPOPT\%(.*?)\%REPOPT\%/s", $datmatches[1], $optmatches);
  $rep_cont = "";

  $ID[0] = substr($_GET["uid"], strpos($_GET["uid"], "ac") + 2,strrpos($_GET["uid"], "ac") - strpos($_GET["uid"], "ac") - 2);
  $ID[1] = substr($_GET["uid"], strrpos($_GET["uid"], "ac") + 2);

  if ($new_id > -1){
    $ID[1] = $new_id;
    $_GET["uid"] = "ac".$ID[0]."ac".$ID[1];
  };

  getData("SELECT * FROM term t, class c ".
          "WHERE c.term_id=t.id and c.id=".$ID[0], $class_name);
  $class_name["text"] = htmlspecialchars($class_name["text"]);

  getData("SELECT * FROM term t, class c ".
          "WHERE c.term_id=t.id and c.id=".$ID[1], $class_name1);
  $class_name1["text"] = htmlspecialchars($class_name1["text"]);

  getData("SELECT * FROM term t, class c, ontology o ".
          "WHERE c.onto_id=o.id and o.term_id=t.id and c.id=".$ID[0], $onto_name);
  $onto_name["text"] = htmlspecialchars($onto_name["text"]);

  $childs1 = Array();$childs = Array();
  if ($ID[1] == -1){
    get_related_class($childs1, $ID[0]);
    $childs_acons = Array(); $count = 0;
    get_acon_child($ID[0], $childs_acons, $count);
    $k = 0;
    for ($i = 0; $i < count($childs1); $i++){
      $b = true;
      for ($j = 0; $j < count($childs_acons); $j++){
        if ($childs1[$i]["id"] == $childs_acons[$j]["id"])
          $b = false;
      };
      if ($b){
          $childs[$k] = $childs1[$i];
          $k++;
        };
    };
    $childs1 = $childs;

    $inh_acons = Array();$childs = Array();
    $count = 0;$k = 0;
    get_acon_inherited($ID[0], $inh_acons, $count);
    for ($i = 0; $i < count($childs1); $i++){
      $b = true;
      for ($j = 0; $j < count($inh_acons); $j++){
        if ($childs1[$i]["id"] == $inh_acons[$j])
          $b = false;
      };
      if ($b){
          $childs[$k] = $childs1[$i];
          $k++;
        };

    };
    if (count($inh_acons) == 0)
      $childs = $childs1;

  //Test of hierarchy
      include "hierarchy.php";
      $childs1 = $childs;
      $inh_h = Array();$childs = Array();
      $count = 0;$k = 0;
      get_h_inherited($ID[0], $inh_h, $count);
      for ($i = 0; $i < count($childs1); $i++){
        $b = true;
        for ($j = 0; $j < count($inh_h); $j++){
          if ($childs1[$i]["id"] == $inh_h[$j])
            $b = false;
        };
        if ($b){
            $childs[$k] = $childs1[$i];
            $k++;
          };

      };
      if (count($inh_h) == 0)
        $childs = $childs1;
   };

  $udata = "";
  if ($data["type"] >= 1) { //user
    //New name
    if ($new_id > -1){
      $contents = str_replace("%NAME%", $class_name1["text"], $contents);
      $contents = str_replace("%ID%", $_GET["uid"], $contents);
      $contents = str_replace("%DatHref%", "index.php?trg=ontoedit&amp;act=acondata&amp;sid=".
        $session_id."&amp;uid=".$_GET["uid"], $contents);
    };

    //Class Name
    $classlb = str_replace($optmatches[0], "", $datmatches[1]);
    $classlb = str_replace("%TYPE%", "Label", $classlb);
    $classlb = str_replace("%NODE_ID%", "cname", $classlb);
    $classlb = str_replace("%NODE_TL%", getMessage("Class Name"), $classlb);
    $classlb = str_replace("%VALUE%", $class_name["text"], $classlb);

    $iclassid = str_replace($optmatches[0], "", $datmatches[1]);
    $iclassid = str_replace("%TYPE%", "Hidden", $iclassid);
    $iclassid = str_replace("%NODE_ID%", "ohidd", $iclassid);
    $iclassid = str_replace("%NODE_TL%", "", $iclassid);
    $iclassid = str_replace("%VALUE%", $ID[0], $iclassid);

    $iclass2id = str_replace($optmatches[0], "", $datmatches[1]);
    $iclass2id = str_replace("%TYPE%", "Hidden", $iclass2id);
    $iclass2id = str_replace("%NODE_ID%", "ohidd2", $iclass2id);
    $iclass2id = str_replace("%NODE_TL%", "", $iclass2id);
    $iclass2id = str_replace("%VALUE%", $ID[1], $iclass2id);

    //Ontology Name
    $iclasslb = str_replace($optmatches[0], "", $datmatches[1]);
    $iclasslb = str_replace("%TYPE%", "Label", $iclasslb);
    $iclasslb = str_replace("%NODE_ID%", "oname", $iclasslb);
    $iclasslb = str_replace("%NODE_TL%", getMessage("Ontology Name"), $iclasslb);
    $iclasslb = str_replace("%VALUE%", $onto_name["text"], $iclasslb);

    if ((getdata("SELECT * FROM acon WHERE (class1_id=".$ID[0]." and class2_id=".$ID[1].")", $onto_data) == 1)&&(count($childs) > 0)&&($ID[1] == -1)){
      $opts = "";
      for ($i = 0; $i < count($childs); $i++) {
        $childs[$i]["text"] = htmlspecialchars($childs[$i]["text"]);
        $opt = str_replace("%OPT_ID%", $childs[$i]["id"], $optmatches[1]);
        $opt = str_replace("%OPT_TL%", $childs[$i]["text"], $opt);
        if ($childs[$i]["id"] == $ID[1])
          $opt = str_replace("%SELECTED%", "selected", $opt);
        else
          $opt = str_replace("%SELECTED%", "0", $opt);
        $opts .= $opt;
      }
      //domain
      $utype = str_replace($optmatches[0], $opts, $datmatches[1]);
      $utype = str_replace("%TYPE%", "Option", $utype);
      $utype = str_replace("%NODE_ID%", "otype", $utype);
      $utype = str_replace("%NODE_TL%", getMessage("Related Class"), $utype);

      $ubutn = str_replace($optmatches[0], "", $datmatches[1]);
      $ubutn = str_replace("%TYPE%", "Button", $ubutn);
      $ubutn = str_replace("%NODE_ID%", "obutn", $ubutn);
      $ubutn = str_replace("%NODE_TL%", getMessage("Save"), $ubutn);
      $ubutn = str_replace("%VALUE%", "'0', 'otype', 'ohidd', 'ohidd2'", $ubutn);
    }else{
      getData("SELECT t.text FROM term t, acon a "."WHERE a.constr_name = t.id and t.lang_id = ".$language["content"]." and ((a.class1_id = ".$ID[0]." and a.class2_id = ".$ID[1].") or (a.class1_id = ".$ID[1]." and a.class2_id = ".$ID[0]."))", $constr_info);

      $constr_name = str_replace($optmatches[0], "", $datmatches[1]);
      $constr_name = str_replace("%TYPE%", "Text", $constr_name);
      $constr_name = str_replace("%NODE_ID%", "oconstrname", $constr_name);
      $constr_name = str_replace("%NODE_TL%", getMessage("Relationship Name"), $constr_name);
      $constr_name = str_replace("%VALUE%", $constr_info["text"], $constr_name);
      $utype = $constr_name;
      $ubutn = str_replace($optmatches[0], "", $datmatches[1]);
      $ubutn = str_replace("%TYPE%", "Button", $ubutn);
      $ubutn = str_replace("%NODE_ID%", "obutn", $ubutn);
      $ubutn = str_replace("%NODE_TL%", getMessage("Save"), $ubutn);
      $ubutn = str_replace("%VALUE%", "'0', 'ohidd', 'ohidd2', 'oconstrname'", $ubutn);

    };
    $udata = str_replace($datmatches[0], $classlb.$iclasslb.$utype.$ubutn.$iclassid.$iclass2id, $matches[1]);
    $udata = str_replace("%GROUP_ID%", "0", $udata);
    $udata = str_replace("%GROUP_TL%", getMessage("Associative Constraints"), $udata);

//Action
    $btn1 = str_replace($optmatches[0], "", $datmatches[1]);
    $btn1 = str_replace("%TYPE%", "Button", $btn1);
    $btn1 = str_replace("%NODE_ID%", "", $btn1);
    $btn1 = str_replace("%NODE_TL%", getMessage("Ontologies"), $btn1);
    $btn1 = str_replace("%VALUE%", "'wd_tree', 'index.php?trg=ontoedit&amp;sid=".
      $session_id."&amp;act=ontolist&amp;sel=".$onto_name["id"]."'", $btn1);

    $btns = str_replace($datmatches[0], $btn1, $matches[1]);
    $btns = str_replace("%GROUP_ID%", "1", $btns);
    $btns = str_replace("%GROUP_TL%", getMessage("Action"), $btns);
  }
  else { //guest
/*    //New name
    $contents = str_replace("%NAME%", $ontodata["name"], $contents);
    //Name
    $fname = str_replace($optmatches[0], "", $datmatches[1]);
    $fname = str_replace("%TYPE%", "Label", $fname);
    $fname = str_replace("%NODE_ID%", "oname", $fname);
    $fname = str_replace("%NODE_TL%", getMessage("Name"), $fname);
    $fname = str_replace("%VALUE%", $ontodata["name"], $fname);
    //Description
    $lname = str_replace($optmatches[0], "", $datmatches[1]);
    $lname = str_replace("%TYPE%", "Label", $lname);
    $lname = str_replace("%NODE_ID%", "odescr", $lname);
    $lname = str_replace("%NODE_TL%", getMessage("Description"), $lname);
    $lname = str_replace("%VALUE%", $ontodata["description"], $lname);
    //User
    $udata = str_replace($datmatches[0], $fname.$lname, $matches[1]);
    $udata = str_replace("%GROUP_ID%", "0", $udata);
    $udata = str_replace("%GROUP_TL%", getMessage("Attribute data"), $udata);
    //print "****".$udata."****";*/
  }
  header ($header["xml"]);
  return str_replace($matches[0], $udata.$btns, $contents);
}

function get_acon_inherited($class_id, &$inh_acons, &$count){
  $i_max = getMultData("SELECT * FROM acon WHERE class1_id=".$class_id, $data);
  for ($i = 0; $i < $i_max; $i++){
    $inh_acons[$count] = $data[$i]["class2_id"];
    $count++;
  };

/*  $i_max = getMultData("SELECT * FROM acon WHERE class2_id=".$class_id, $data);
  for ($i = 0; $i < $i_max; $i++){
    $inh_acons[$count] = $data[$i]["class1_id"];
    $count++;
  };
*/
  getData("SELECT parent_id FROM class WHERE id=".$class_id, $parentdata);
  getData("SELECT * FROM class c, term t WHERE t.id=c.term_id and c.id=".$parentdata["parent_id"], $parentdata1);
  if ($parentdata1["text"] != "Thing")
    get_acon_inherited($parentdata["parent_id"], $inh_acons, $count);
};

function get_acon_child($class_id, &$child_acons, &$count){
  $i_max = getMultData("SELECT * FROM acon WHERE class1_id=".$class_id, $data);
  for ($i = 0; $i < $i_max; $i++){
    $child_acons[$count]["id"] = $data[$i]["class2_id"];
    $child_acons[$count]["class_id"] = $data[$i]["class1_id"];
    $count++;
  };

/*  $i_max = getMultData("SELECT * FROM acon WHERE class2_id=".$class_id, $data);
  for ($i = 0; $i < $i_max; $i++){
    $child_acons[$count]["id"] = $data[$i]["class1_id"];
    $child_acons[$count]["class_id"] = $data[$i]["class2_id"];
    $count++;
  };
*/
  $i_max = getMultData("SELECT id FROM class WHERE parent_id=".$class_id, $childdata);
  for ($i = 0; $i < $i_max; $i++)
    get_acon_child($childdata[$i]["id"], $child_acons, $count);
};

function createAcon() {
  global $_GET, $data, $path, $style, $session_id, $language, $header;
  if (strrpos(substr($_GET["uid"], 2), "ac")==false)
    $_GET["uid"] = substr($_GET["uid"], 2);
  else
    $_GET["uid"] = substr($_GET["uid"], 2, strrpos($_GET["uid"], "ac")-2);

  if ($data["type"] >= 1) { //user
    $filename = $path[$style]["tpl"]."/action.xml";
    $fp = fopen($filename, "r");
    $contents = fread($fp, filesize ($filename));
    fclose ($fp);
    preg_match("/\%REPEAT\%(.*?)\%REPEAT\%/s", $contents, $matches);

    $sql_tmp = "SELECT type_id FROM ontology WHERE id = (SELECT onto_id FROM class WHERE id = ".$_GET["uid"].")";
    $ontotype = Array();
    getData($sql_tmp, $ontotype);
    if (!($ontotype["type_id"] == "5")){
      if (getdata("SELECT * FROM acon WHERE (class1_id=".$_GET["uid"]." and class2_id=-1) or (class2_id=".$_GET["uid"]." and class1_id=-1)", $data) != 1){

        if (getData("SELECT onto_id FROM class where id = ".$_GET["uid"].";", $odatatmp) != 1) return "";
        insertRecord("INSERT INTO acon (onto_id, class1_id, class2_id, session_id) VALUES(".
          $odatatmp["onto_id"].", ".$_GET["uid"].", -1, '".
          $session_id."');");

        if (getData("SELECT id FROM acon where session_id = '".$session_id."';", $datatmp) == 1) {
          $sql_str = "UPDATE acon SET session_id = '' WHERE id=".
          $datatmp["id"].";";
          insertRecord($sql_str);
          $contents = str_replace("%SCCSS%", "1", $contents);
          $childs[0]["id"] = -1;
        }else{
          $childs[0]["id"] = "0";
          $contents = str_replace("%SCCSS%", "0", $contents);
          $datatmp["id"] = 0;
        };
      }else{
          $childs[0]["id"] = "0";
          $contents = str_replace("%SCCSS%", "0", $contents);
          $datatmp["id"] = 0;
      }
    }else{
      $childs[0]["id"] = "0";
      $contents = str_replace("%SCCSS%", "0", $contents);
      $datatmp["id"] = 0;
    };
  } else {
    $childs[0]["id"] = "0";
    $contents = str_replace("%SCCSS%", "0", $contents);
    $datatmp["id"] = 0;
  }
  $tmp_cont = str_replace("%NODE_ID%", "ac".$_GET["uid"]."ac".$childs[0]["id"], $matches[1]);
  $tmp_cont = str_replace("%NODE_NM%", getMessage("Name"), $tmp_cont);
  $tmp_cont = str_replace("%IMG_TYPE%", "class", $tmp_cont);
  $tmp_cont = str_replace("%NODE_LN%", "index.php?trg=ontoedit&amp;act=acondata&amp;sid=".
    $session_id."&amp;uid=com".$childs[0]["id"], $tmp_cont);
  $contents = str_replace($matches[0], $tmp_cont, $contents);
  $contents = str_replace("%XMLSRC%", "index.php?trg=ontoedit&amp;act=aconlist&amp;sid=".
    $session_id."&amp;uid=ac".$_GET["uid"]."&amp;sel=ac".$_GET["uid"]."ac".$childs[0]["id"], $contents);
  $contents = str_replace("%ACT_ID%", "Add", $contents);
  $contents = str_replace("%PARENT%", "ac".$_GET["uid"], $contents);
  header ($header["xml"]);
  return $contents;
}

function deleteAcon($ID) {
  global $data, $path, $style, $session_id, $header;

  $filename = $path[$style]["tpl"]."/action.xml";
  $fp = fopen($filename, "r");
  $contents = fread($fp, filesize ($filename));
  fclose ($fp);

  preg_match("/\%REPEAT\%(.*?)\%REPEAT\%/s", $contents, $matches);
  if (isset($ID[1])){
    $count = 0; $inh_acons = Array();
    getData("SELECT parent_id FROM class WHERE id=".$ID[0], $parentdata);
    getData("SELECT * FROM class c, term t WHERE t.id=c.term_id and c.id=".$parentdata["parent_id"], $parentdata1);
    if ($parentdata1["text"] != "Thing")
      get_acon_inherited($parentdata["parent_id"], $inh_acons, $count);
    $b = true;
    for ($i = 0; $i < count($inh_acons); $i++)
      if ($inh_acons[$i] == $ID[1])
        $b = false;
  };

  $sql_tmp = "SELECT type_id FROM ontology WHERE id = (SELECT onto_id FROM class WHERE id = ".$ID[0].")";
  $ontotype = Array();
  getData($sql_tmp, $ontotype);
  if (!($ontotype["type_id"] == "5")){
    //echo "Del Acon process ".$b."<br />";
    if (($data["type"] >= 1 && isset($ID[1]))/*&&($b)*/){
        //Delete compatible
        $childs_com = Array(); $count = 0; $k = 0;
        com_child($ID[0], $childs_com, $count);
        for ($j = 0; $j < count($childs_com); $j++)
          if ($ID[1] == $childs_com[$j]["id"])
            insertRecord("DELETE FROM compatible WHERE (class1_id=".$childs_com[$j]["id"]." and class2_id=".$childs_com[$j]["class_id"].") or (class1_id=".$childs_com[$j]["class_id"]." and class2_id=".$childs_com[$j]["id"].");");
  /*
        insertRecord("DELETE FROM acon WHERE (class1_id=".$ID[1]." and class2_id=".$ID[0].") or (class1_id=".$ID[0]." and class2_id=".$ID[1].");");
        $contents = str_replace("%SCCSS%", "1", $contents);
  */
        insertRecord($la_sql = "DELETE FROM acon WHERE (class1_id=".$ID[0]." and class2_id=".$ID[1].") or (class2_id=".$ID[0]." and class1_id=".$ID[1].")");
        //echo "Del Acon complite ".$ID[0]."<br />";
        $contents = str_replace("%SCCSS%", "1", $contents);
    }else{
      $contents = str_replace("%SCCSS%", "0", $contents);
      $ID[1] = 0;
    }
  }else{
    $contents = str_replace("%SCCSS%", "0", $contents);
    $ID[1] = 0;
  };

  $contents = str_replace($matches[0], "", $contents);
  $contents = str_replace("%ACT_ID%", "Delete", $contents);
  $contents = str_replace("%PARENT%", "ac".$ID[0]."ac".$ID[1], $contents);
  $contents = str_replace("%SELECT%", "ac".$ID[0], $contents);
  header ($header["xml"]);
  return $contents;
}


function updateAcon() {
  global $_GET, $data, $path, $style, $session_id, $term_Thing_ID, $language, $header;
  $comment = "";
  if ($data["type"] >= 1 && isset($_GET["otype"])) {
    $_GET["ohidd"] = utf8Encode($_GET["ohidd"]);
    $_GET["ohidd2"] = utf8Encode($_GET["ohidd2"]);
    $_GET["otype"] = utf8Encode($_GET["otype"]);

    $sql_str = "UPDATE acon SET class2_id = ".$_GET["otype"].
      " WHERE class1_id=".$_GET["ohidd"]." and class2_id=".$_GET["ohidd2"];
    insertRecord($sql_str);

    $sql_str = "UPDATE acon SET class1_id = ".$_GET["otype"].
      " WHERE class2_id=".$_GET["ohidd"]." and class1_id=".$_GET["ohidd2"];
    insertRecord($sql_str);
  };
  if ($data["type"] >= 1 && isset($_GET["oconstrname"])) {
    $_GET["ohidd"] = utf8Encode($_GET["ohidd"]);
    $_GET["ohidd2"] = utf8Encode($_GET["ohidd2"]);
    $_GET["oconstrname"] = utf8Encode($_GET["oconstrname"]);

    $sql_str = "INSERT INTO term (lang_id, text, stem_text) VALUES (".$language["content"].", '".$_GET["oconstrname"]."', '".$_GET["oconstrname"]."')";
    insertRecord($sql_str);
    $term_id = GetPreviousInsertedID();
    $sql_str = "UPDATE acon SET constr_name = ".$term_id." WHERE class1_id=".$_GET["ohidd"]." and class2_id=".$_GET["ohidd2"];
    insertRecord($sql_str);

    $sql_str = "UPDATE acon SET constr_name = ".$term_id." WHERE class1_id=".$_GET["ohidd2"]." and class2_id=".$_GET["ohidd"];
    insertRecord($sql_str);

  };


  header ($header["xml"]);
  $contents = aconData($_GET["otype"]);
  return $contents;
}

?>